package com.hp.servlet;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.hp.entity.MedicalAdvice;
import com.hp.entity.Patient;
import com.hp.entity.dto.MedicalAdviceDto;
import com.hp.entity.dto.RoomDto;
import com.hp.service.MedicalAdviceService;
import com.hp.service.PatientService;
import com.hp.service.RoomService;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.ConvertUtils;
import org.apache.commons.beanutils.converters.DateConverter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@WebServlet(name = "MedicalAdviceServlet", value = "/medicalAdviceServlet")
public class MedicalAdviceServlet extends HttpServlet {
    private ObjectMapper mapper = new ObjectMapper();
    private MedicalAdviceService medicalAdviceService=new MedicalAdviceService();
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        response.setContentType("application/json;charset=UTF-8");
        String method = request.getParameter("method");
        if (method != null && method.equals("getMedicalAdviceByRegistryId")) {
            getMedicalAdviceByRegistryId(request, response);
        }else if (method != null && method.equals("updateMedicalAdviceById")) {
            updateMedicalAdviceById(request, response);
        }else if (method != null && method.equals("insertMedicalAdvice")) {
            insertMedicalAdvice(request, response);
        }


    }

    private void insertMedicalAdvice(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        Map<String, Object> map = new HashMap<>();
        MedicalAdvice medicalAdvice=new MedicalAdvice();
        try {
            BeanUtils.populate(medicalAdvice,request.getParameterMap());
            Date currentDate=new Date();
            medicalAdvice.setUpdatedTime(currentDate);
            medicalAdvice.setCreatedTime(currentDate);
            if(medicalAdviceService.insertMedicalAdvice(medicalAdvice)){
                map.put("code",1001);
                map.put("msg","添加医嘱成功");
            }else{
                map.put("code",1002);
                map.put("code","添加医嘱失败");
            }
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        String json = mapper.writeValueAsString(map);
        response.getWriter().print(json);
    }

    //更新医嘱
    private void updateMedicalAdviceById(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Map<String, Object> map = new HashMap<>();
        MedicalAdvice medicalAdvice=new MedicalAdvice();
        try {
            BeanUtils.populate(medicalAdvice,request.getParameterMap());
            Date currentDate=new Date();
            medicalAdvice.setUpdatedTime(currentDate);
            if(medicalAdviceService.updateMedicalAdviceById(medicalAdvice)){
                map.put("code",1001);
                map.put("msg","更新医嘱成功");
            }else{
                map.put("code",1002);
                map.put("code","更新医嘱失败");
            }
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        String json = mapper.writeValueAsString(map);
        response.getWriter().print(json);
    }

    //验证身份证
    private void getMedicalAdviceByRegistryId(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Map<String, Object> map = new HashMap<>();
        String registryId=request.getParameter("registryId");
        if(registryId!=null){
            MedicalAdviceDto dto=medicalAdviceService.getMedicalAdviceByRegistryId(Integer.parseInt(registryId));
            map.put("medivalAdvice",dto);
            String json = mapper.writeValueAsString(map);
            response.getWriter().print(json);
        }

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
}
